/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 10/06/2004
 * Time: 20:19:41
 */
package com.talios.jira.idea.intentions;

import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import com.talios.jira.browser.IssueComment;
import com.talios.jira.browser.JiraItem;

public class JiraIssueCommentIntention extends AnnotatedIssueCommentIntention {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private final Project myProject;

    public JiraIssueCommentIntention(Project project) {
        myProject = project;
    }

    public void projectOpened() {
    }

    public void projectClosed() {
    }

    public String getComponentName() {
        return "JiraIssueCommentIntention";
    }

    public void initComponent() {
        IntentionManager.getInstance(myProject).addAction(this);
    }

    public void disposeComponent() {
    }

    public String getText() {
        return "Comment on issue " + issueKey;
    }

    public String getFamilyName() {
        return getText();
    }

    public void invoke(Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {

        JiraItem item = findAnnotatedIssue(editor, psiFile, project);

        if (item != null) {
            LOGGER.info("Commenting on issue " + issueKey + " at " + item.getProject().getServer().getBaseurl());
            IssueComment issueComment = new IssueComment(project);
            issueComment.commentIssue(item);
        }

    }

    public boolean startInWriteAction() {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
